function header(map) {
    var arr = [];

    arr.push('package ' + map.attach.controllerPackage + ';');
    arr.push('');
    arr.push('import cn.seaboot.admin.mvc.Pagination;');
    arr.push('import cn.seaboot.admin.mvc.argument.Params;');
    arr.push('import com.github.pagehelper.PageHelper;');
    arr.push('import com.github.pagehelper.PageInfo;');
    arr.push('import io.swagger.v3.oas.annotations.Operation;');
    arr.push('import io.swagger.v3.oas.annotations.Parameter;');
    arr.push('import io.swagger.v3.oas.annotations.Parameters;');
    arr.push('import io.swagger.v3.oas.annotations.tags.Tag;');
    arr.push('import org.springframework.stereotype.Controller;');
    arr.push('import org.springframework.validation.annotation.Validated;');
    arr.push('import org.springframework.web.bind.annotation.RequestMapping;');
    arr.push('import org.springframework.web.bind.annotation.RequestMethod;');
    arr.push('import org.springframework.web.bind.annotation.RequestParam;');
    arr.push('import org.springframework.web.bind.annotation.ResponseBody;');
    arr.push('');
    arr.push('import javax.annotation.Resource;');
    arr.push('import java.util.List;');
    arr.push('import java.util.Map;');
    arr.push('');
    arr.push('/**');
    arr.push(' * ' + map.tableComment + ' [' + map.tableName + '] Controller');
    arr.push(' * @author Mr.css on 2018-6-20');
    arr.push(' */');
    arr.push('@Controller');
    arr.push('@Tag(name = "' + map.tableComment + ' [' + map.tableName + '] 管理")');
    arr.push('@RequestMapping("' + map.camelName + '")');
    arr.push('public class ' + map.pascalName + 'Ctrl {');
    arr.push('');
    arr.push('@Resource');
    arr.push('    private ' + map.pascalName + 'Service ' + map.camelName + 'Service;');
    arr.push('');

    return arr.join("\r\n");
}

function insert(map) {
    var arr = [];
    arr.push('');
    arr.push('    /**');
    arr.push('     * ' + map.tableComment + ' - 增');
    arr.push('     *');
    arr.push('     * @param pojo pojo');
    arr.push('     * @return affected rows');
    arr.push('     */');
    arr.push('    @ResponseBody');
    arr.push('    @Operation(description = "' + map.tableComment + ' [' + map.tableName + '] 增")');
    arr.push('    @RequestMapping(value = "info", method = RequestMethod.POST)');
    arr.push('    public int saveInfo(@Validated ' + map.pascalName + ' pojo) {');
    arr.push('        return ' + map.camelName + 'Service.insert(pojo);');
    arr.push('    }');
    arr.push('');
    return arr.join("\r\n");
}

function remove(map) {
    var arr = [];
    arr.push('');
    arr.push('    /**');
    arr.push('     * ' + map.tableComment + ' - 删');
    arr.push('     *');
    arr.push('     * @param id id');
    arr.push('     * @return affected rows');
    arr.push('     */');
    arr.push('    @ResponseBody');
    arr.push('    @Operation(description = "' + map.tableComment + ' [' + map.tableName + '] 删")');
    arr.push('    @RequestMapping(value = "info", method = RequestMethod.DELETE)');
    arr.push('    public int deleteInfo(@Params Long id) {');
    arr.push('        return ' + map.camelName + 'Service.deleteById(id);');
    arr.push('    }');
    arr.push('');
    return arr.join("\r\n");
}

function modify(map) {
    var arr = [];
    arr.push('');
    arr.push('    /**');
    arr.push('     * ' + map.tableComment + ' - 改');
    arr.push('     *');
    arr.push('     * @param pojo pojo');
    arr.push('     * @return affected rows');
    arr.push('     */');
    arr.push('    @ResponseBody');
    arr.push('    @Operation(description = "' + map.tableComment + ' [' + map.tableName + '] 改")');
    arr.push('    @RequestMapping(value = "info", method = RequestMethod.PUT)');
    arr.push('    public int updateInfo(@Validated ' + map.pascalName + ' pojo, @Params Long id) {');
    arr.push('        return ' + map.camelName + 'Service.updateById(pojo);');
    arr.push('    }');
    arr.push('');
    return arr.join("\r\n");
}

function modifyField(map) {
    var arr = [];
    arr.push('');
    arr.push('    /**');
    arr.push('     * ' + map.tableComment + '- 改');
    arr.push('     *');
    arr.push('     * @param arg 字段编辑');
    arr.push('     * @return pojo');
    arr.push('     */');
    arr.push('    @ResponseBody');
    arr.push('    @Operation(description = "' + map.tableComment + ' [' + map.tableName + '] 改")');
    arr.push('    @RequestMapping(value = "edit", method = RequestMethod.POST)');
    arr.push('    public int updateInfo(@Validated ModifyArgument arg) {');
    arr.push('        return ' + map.camelName + 'Service.updateFieldById(arg);');
    arr.push('    }');
    arr.push('');
    return arr.join("\r\n");
}

function queryById(map) {
    var arr = [];
    arr.push('');
    arr.push('    /**');
    arr.push('     * ' + map.tableComment + '- 查');
    arr.push('     *');
    arr.push('     * @param id id');
    arr.push('     * @return pojo');
    arr.push('     */');
    arr.push('    @ResponseBody');
    arr.push('    @Operation(description = "' + map.tableComment + ' [' + map.tableName + '] 查")');
    arr.push('    @RequestMapping(value = "info", method = RequestMethod.GET)');
    arr.push('    public ' + map.pascalName + ' queryById(@Params String id) {');
    arr.push('        return ' + map.camelName + 'Service.queryById(id);');
    arr.push('    }');
    arr.push('');
    return arr.join("\r\n");
}

function queryList(map) {
    var arr = [];
    arr.push('');
    arr.push('    /**');
    arr.push('     * ' + map.tableComment + '- 查列表');
    arr.push('     *');
    arr.push('     * @param params 参数');
    arr.push('     * @return PageInfo');
    arr.push('     */');
    arr.push('    @ResponseBody');
    arr.push('    @Operation(description = "' + map.tableComment + ' [' + map.tableName + '] 查列表")');
    arr.push('    @RequestMapping(value = "page", method = RequestMethod.GET)');
    arr.push('    public PageInfo<' + map.pascalName + '> queryList(@RequestParam Map<String, Object> params) {');
    arr.push('        return ' + map.camelName + 'Service.queryList(params);');
    arr.push('    }');
    arr.push('');
    return arr.join("\r\n");
}

function footer(map) {
    return "}\r\n";
}

function main(map) {
    var arr = [];
    arr.push(header(map));
    arr.push(insert(map));
    arr.push(remove(map));
    arr.push(modify(map));
    arr.push(modifyField(map));
    arr.push(queryById(map));
    arr.push(queryList(map));
    arr.push(footer(map));
    return arr.join('');
}

export default main;
